<?php

namespace app\admin\model\facrm\product;

use think\Model;
use traits\model\SoftDelete;

class Unit extends Model {

    use SoftDelete;

    // 表名
    protected $name = 'facrm_product_unit';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    // 追加属性
    protected $append = [
    ];

    public function object_array($array) {
        if (is_object($array)) {
            $array = (array) $array;
        } if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
    /**
     * 根据名称获取ID
     * @param $name 名称
     * @param bool $is_insert 如果不存在是否插入
     * @return int 返回的ID
     */
    public static function getIdByName($name,$is_insert=false){
        static $typeids = [];
        if (isset($typeids[$name])) {
            return $typeids[$name];
        }
        $typeid=self::where('name',$name)->value('id');
        if ($typeid){
            $typeids[$name] = $typeid;
        }elseif (!$typeid&&$is_insert){
            //插入分类
            $typeid=self::insertGetId([
                'name'=>$name,
                'create_time'=>time(),
                'update_time'=>time(),
            ]);
            $typeid?$typeids[$name] = $typeid:'';
        }
        return $typeid;
    }

}
